import { useMemo, FC } from 'react';
import {
  useBillParams,
  useNoticeVoucherEvent,
  useBillBreadcrumbEvent,
} from '@/pages/invoice/hooks';

import { WithNormalRender } from "@ey-render/hoc-form";

import { refresh } from '@/pages/invoice/service';

import { ArcvBorrowConfig, apiUrl, ArcvBorrowI18N } from './service';
import { onFormInit } from '@formily/core';

import components from "@/components/schema-form-render/schema-components-registed/components-biz";

interface ArcvBorrowDetailInvoiceProps {}

const ArcvBorrowDetailInvoice: FC<ArcvBorrowDetailInvoiceProps> = (props) => {
  // 处理 各个入口单据 参数不一致问题
  const { code, needFooter, type, pageType } = useBillParams(props);
  // 通知 BasicLayout 生成对应的面包屑
  useBillBreadcrumbEvent(ArcvBorrowI18N[type] || ArcvBorrowI18N, type);

  // 通知子组件刷新预制凭证
  const noticeVoucherEvent = useNoticeVoucherEvent();

  const detailApi = useMemo(() => {
    return code
      ? {
          remoteUrl: apiUrl.detail,
          method: 'POST',
          remoteParams: {
            code,
          },
          pageCode: ArcvBorrowConfig.DETAIL,
        }
      : {};
  }, [code]);

  const BillFormLayout = useMemo(() => {
    return WithNormalRender({
      components: {
        ...components
      }
    });
  }, []);

  const refreshInvoice = async () => {
    const res: any = await refresh({ code: code, pageCode: ArcvBorrowConfig.EDIT });
    const { success } = res || {};
    if (success && code) {
      noticeVoucherEvent(code);
    }
  };

  return (
    <BillFormLayout
      anchor
      type="DETAIL"
      {...detailApi}
      formConfigs={{
        effects: () => {
          onFormInit((form) => {
            form.setFieldState('*(arcvItems.operations,arcvBoxItems.operations)', field => {
              field.visible = false;
            })
          })
        }
      }}
      flag={code}
      filedConfig={{}}
      needFooter={needFooter}
      events={{
        refreshInvoice,
      }}
    />
  );
};

export default ArcvBorrowDetailInvoice;
